package org.ns.summer.classpath.classreader;

import java.io.InputStream;

import org.ns.summer.classpath.classreader.constantinfo.*;

// 定义所有常量的父抽象、子类实现抽象方法read来读取自己所需的内容
public abstract class ConstantInfo {
    public static final short TAG_Class = 7;
    public static final short TAG_Fieldref = 9;
    public static final short TAG_Methodref = 10;
    public static final short TAG_InterfaceMethodref = 11;
    public static final short TAG_String = 8;
    public static final short TAG_Integer = 3;
    public static final short TAG_Float = 4;
    public static final short TAG_Long = 5;
    public static final short TAG_Double = 6;
    public static final short TAG_NameAndType = 12;
    public static final short TAG_Utf8 = 1;
    public static final short TAG_MethodHandle = 15;
    public static final short TAG_MethodType = 16;
    public static final short TAG_InvokeDynamic = 18;

    //public short tag;

    // 子类实现读取所需内容
    public abstract void read(InputStream inputStream);

    // 通过tag 确定是哪一类ConstantInfo
    public static ConstantInfo getConstantInfo(short tag) {
        switch (tag) {
            case TAG_Class:
               return new ConstantClass();   
            case TAG_Fieldref:
            case TAG_Methodref:
            case TAG_InterfaceMethodref:
                return new ConstantMemberRef();
            case TAG_Long:
                return new ConstantLong();
            case TAG_Double:
                return new ConstantDouble();
            case TAG_String:
                return new ConstantString();
            case TAG_Integer:
                return new ConstantInteger();
            case TAG_Float:
                return new ConstantFloat();
            case TAG_NameAndType:
                return new ConstantNameAndType();
            case TAG_Utf8:
                return new ConstantUtf8();
            case TAG_MethodHandle:
                return new ConstantMethodHandle();
            case TAG_MethodType:
                return new ConstantMethodType();
            case TAG_InvokeDynamic:
                return new ConstantInvokeDynamic();
        }
        return null;
    }

}
